﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using System.Data.Linq;
namespace BAL
{
    public class SchoolSet : BaseBAL<SchoolSetInfo, Models.SchoolSet>
    {
        private static string CacheName = "AllSchoolSet";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.SchoolSets.ToList(), 0, 60);
            }
        }

        /// <summary>
        /// 取得一条
        /// </summary>
        /// <param name="InputId">信息ID</param>
        /// <returns></returns>
        public static SchoolSetInfo GetInfoBySchoolId(int InputId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.SchoolSet> schoolsetInfo = (IList<Models.SchoolSet>)CacheHelper.GetCache(CacheName);
            if (schoolsetInfo.Where(b => b.SchoolId == InputId).Count() > 0)
            {
                return TransT2ToT1(schoolsetInfo.Single(b => b.SchoolId == InputId));
            }
            else
                return null;
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static SchoolSetInfo Save(SchoolSetInfo InstInfo)
        {
            SchoolSetInfo schoolsetInfo = InstInfo;
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
            }
            else
            {
                schoolsetInfo = Add(InstInfo);
            }
            ToCache();
            return schoolsetInfo;
        }
    }
}
